package controller;

import model.FileModel;
import user.Receive;

import javax.swing.*;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

public class ReceiveFile {
    private  FileModel fileModel;
    public ReceiveFile(FileModel fileModel){
        this.fileModel=fileModel;
        beginReceive();

    }
    public void beginReceive(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Receive.getTextReceiveProcess().setText("正在开始接收....");
                    ServerSocket serverSocket=new ServerSocket(9999);
                    Socket socket = serverSocket.accept();   //阻塞
                    //得到数据 开始建立连接
                    /**
                     传输Byte文件,可使用文件流来传递byte数组
                     FileInputStream in = (FileInputStream)socket.getInputStream();
                     byte[] type=new byte[1024];
                     int length=in.read(type);//将字节读入到数组里面
                     String name=new String(type);
                     System.out.println(name);
                     通过服务器接收文本信息
                     */
                    //******************************文件类型传递************************
                    FileInputStream in = (FileInputStream)socket.getInputStream();
                    byte[] type=new byte[1024];
                    int length=in.read(type);//将字节读入到数组里面
                    byte[] types=new byte[length];
                    getType(type,types,length);
                    String name=new String(types,"GBK").replace("","");
                    System.out.println(name.length());
                    //******************************************************************

                    String path=fileModel.getFile().getPath()+"\\"+name;
                  //  System.out.println("保存路径:"+fileModel.getFile().toPath()+"\\"+name);
                    System.out.println("保存路径:"+path);
                    File file=new File(path);

                    FileModel recFile=new FileModel(file);
                    FileOutputStream out=new FileOutputStream(file);
                    byte[] b=new byte[1024];//创建一个容器
                    int n=in.read(b);//表示读入的字节数
                    Date start = new Date();//开始计时
                    while (n!=-1){
                        out.write(b,0,n);
                        n=in.read(b);
                    }
                    Date  end = new Date();
                    long useTime = (end.getTime() - start.getTime())/1000;//得到使用时间的时间戳
                    String hms=String.valueOf(useTime/1000);
                    fileModel.setTime(hms);

                    socket.close();
                    serverSocket.close();
                    in.close();
                    out.close();
                    Receive.getbtnReceiveFile().setEnabled(true);
                    try {
                        System.out.println("Receive:传输完毕  耗时:" + useTime + "秒  平均速度:" + file.length() / 1024 / 1024 / useTime + " MB/S");
                        Receive.getTextReceiveProcess().setText("传输完毕  耗时:"+ useTime+ "秒  平均速度:"+file.length()/1024/1024/useTime+" MB/S");
                        JOptionPane.showMessageDialog(null,"接收到文件!\n文件路径:"+path);
                    }catch (Exception e){
                        System.out.println("速度太快~~~~ 耗时:"+(end.getTime() - start.getTime())+"ms");
                        Receive.getTextReceiveProcess().setText("速度太快~~~~ 耗时:"+(end.getTime() - start.getTime())+"ms");
                        JOptionPane.showMessageDialog(null,"接收到文件!\n文件路径:"+path);
                    }

                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();

    }

    private void getType(byte[] originbyte,byte[] newbyte,int bytelength) {
        for (int i=0;i<bytelength;i++){
            newbyte[i]=originbyte[i];
        }
    }
}
